<script setup lang="ts">
import {ref} from "vue";
import TnIcon from "@tuniao/tnui-vue3-uniapp/components/icon/src/icon.vue";
import { onMounted } from "vue";
import {getWalletInfoByOpenIdApi, withdrawalProcessingListApi} from "@/api/pay";
import NoData from "@/composables/noData/NoData.vue";
import {onShow} from "@dcloudio/uni-app";
const toPage = () => {
  uni.navigateTo({
    url: `/pages/wallet/detail/index?price=${walletInfo.value.balance}&withdrawDailyLimit=${walletInfo.value.withdrawDailyLimit}&withdrawEveryLimit=${walletInfo.value.withdrawEveryLimit}`
  })
}

type walletInfo = {
  balance: number;
  withdrawnAmount: number;
  toWalletHistoryTwoVOS: Array<any>;
  withdrawDailyLimit: number,
  withdrawEveryLimit: number,
}

const payListV2 = ref([]);
const mchId = ref("");
const withdrawalProcessingList = async () => {
  const res = await withdrawalProcessingListApi();
  if (res?.code === 200) {
    payListV2.value = res?.data?.list || [];
    mchId.value = res?.data?.mchId || "";
  } else {
    payListV2.value = [];
    mchId.value = "";
  }
  console.log(payListV2.value)
}

/**
 * 获取展示数据
 */
const walletInfo = ref<walletInfo>({
  balance: 0,
  withdrawnAmount: 0,
  toWalletHistoryTwoVOS: [],
  withdrawDailyLimit: 0,
  withdrawEveryLimit: 0,
});
onShow(async () => {
  withdrawalProcessingList();
  const res = await getWalletInfoByOpenIdApi();
  walletInfo.value = res?.data || {};
})

const newHand = (packageInfo: string) => {
  if (uni.canIUse('requestMerchantTransfer')) {
    uni.requestMerchantTransfer({
      mchId: mchId.value,
      appId: "wxa8d26f697d0d840a",
      package: packageInfo,
      success: async () => {
        uni.navigateBack();
        // res.err_msg将在页面展示成功后返回应用时返回ok，并不代表付款成功
        uni.showToast({ title: '提现成功', icon: 'success' })
        setTimeout(async () => {
          withdrawalProcessingList();
          const res = await getWalletInfoByOpenIdApi();
          walletInfo.value = res?.data || {};
        }, 1000 * 2)
      },
      fail: (res: any) => {
        console.log('fail:', res);
        uni.showToast({ title: '提现失败', icon: 'none' })
      },
    });
  } else {
    uni.showModal({
      content: '你的微信版本过低，请更新至最新版本。',
      showCancel: false,
    });
  }
}

const flowTypeText = (flowType: number) => {
  if (flowType === 1) {
    return '一期结算'
  } else if (flowType === 2) {
    return '二期结算'
  } else if (flowType === 3) {
    return '子订单结算'
  } else if (flowType === -1) {
    return '转账到微信零钱'
  }
}

const toLog = () => {
  uni.navigateTo({
    url: '/pages/embodiment/index'
  })
}
</script>

<template>
  <view>
    <view class="header">
      <div class="title-text">账户余额（元）</div>
      <div class="content-box">
        <div class="price-text">{{(walletInfo.balance).toFixed(2)}}</div>
        <div class="pri-btn" @click="toPage"> 提现</div>
      </div>
      <div class="line"></div>
<!--      pages/embodiment/index-->
      <div style="display: flex; align-items: center; justify-content: space-between">
        <div class="title-text mt30px">金额（元）</div>
        <div class="title-text mt30px" @click="toLog">提现记录<TnIcon name="right" size="12px" style="color: #FFFFFF"></TnIcon></div>
      </div>

      <div class="title-text mt10px">{{(walletInfo.withdrawnAmount)?.toFixed(2)}}</div>
    </view>
    <view v-if="payListV2.length > 0" class="payList" style="padding-bottom: 10px">
      <view class="list-title">转账中</view>
      <view class="list">
        <view class="list-item" style="display: flex" v-for="(item, index) in payListV2" :key="index">
          <view style="display: flex">
            <view style="height: 40px; width: 40px; border-radius: 5px; background: linear-gradient(135deg, #5E84DE, #4762F4); text-align: center; line-height: 40px; color: #ffffff">
              <TnIcon name="money" size="30px"></TnIcon>
            </view>
            <view style="margin-left: 10px; font-weight: bold; font-size: 16px">
              <div style="display: flex; align-items: center">转账中 <text style="color: #efa401; font-size: 12px">（未收款24小时退回）</text></div>
              <div style="color: #999999; margin-top: 5px; font-size: 12px;">
                提现金额：<text style="color: red">{{(item.totalAmount).toFixed(2)}}</text> 手续费：<text style="color: red">{{(item.withdrawnAmount).toFixed(2)}}</text>
              </div>
            </view>
          </view>
          <div style="text-align: right" @click="newHand(item.responsePackageInfo)">
            <view style="box-shadow: 1px 1px 2px 1px rgba(0, 0, 0, 0.3); padding: 5px 10px; border-radius: 5px">确认收款</view>
          </div>
        </view>
        <view style="height: 50px"></view>
      </view>
    </view>
    <view class="list-title" v-if="walletInfo?.toWalletHistoryTwoVOS?.length > 0">收支明细</view>
<!--    -->
    <view  class="list" v-if="walletInfo?.toWalletHistoryTwoVOS?.length > 0">
      <view>
        <view class="list-item" style="display: flex" v-for="item in walletInfo?.toWalletHistoryTwoVOS || []" :key="item.projectId">
          <view style="display: flex">
            <view v-if="item.flowType === 1" style="height: 40px; width: 40px; border-radius: 5px; background: linear-gradient(135deg, #07C160, #00D976); text-align: center; line-height: 40px; color: #FFFFFF">
              <TnIcon :name="item.flowType === 1 ? 'payment-wechat' : 'money'" size="30px"></TnIcon>
            </view>
            <view v-else style="height: 40px; width: 40px; border-radius: 5px; background: linear-gradient(135deg, #f44141, #f6aeae); text-align: center; line-height: 40px; color: #FFFFFF">
              <TnIcon name="money" size="30px"></TnIcon>
            </view>
            <view style="margin-left: 10px; font-weight: bold; font-size: 14px">
              <view>{{item.orderName}}（{{ flowTypeText(item.incomeType)}}）</view>
              <view class="mr4px" style="font-size: 12px; font-weight: 500; color: #00D976" v-if="item.incomeType === -1">
                提现成功(服务费{{item.feeAmount}}元)
              </view>
              <view style="color: #999999; margin-top: 5px; font-size: 12px;">{{item.createTime || ''}}</view>
            </view>
          </view>
          <view style="text-align: right" v-if="item.incomeType === -1">
            <view class="mr4px" style="font-size: 18px; font-weight: bold; color: #efa401" v-if="item.incomeType === -1">-{{(item.expenditureAmount).toFixed(2)}}</view>
          </view>
          <view style="text-align: right" v-else>
            <view class="mr4px" style="font-size: 18px; font-weight: bold; color: #efa401" v-if="item.flowType === 0">+{{(item.receiptAmount).toFixed(2)}}</view>
          </view>
        </view>
        <view style="height: 50px"></view>
      </view>

    </view>
    <view v-else-if="walletInfo?.toWalletHistoryTwoVOS?.length === 0 && payListV2.length === 0">
      <no-data message="暂无明细"></no-data>
    </view>
  </view>
</template>

<style scoped lang="scss">
.header {
  width: calc(100vw - 64rpx);
  border-radius: 8px;
  background: linear-gradient(115deg, #5E84DE 0%, #5E81F5 29%, #4562F3 76%, #4762F4 101%);
  margin: 32rpx;
  padding: 48rpx;
  .title-text {
    font-size: 24rpx;
    font-weight: 500;
    color: #ffffff;
  }
  .content-box {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-top: 24rpx;
    .price-text {
      font-size: 56rpx;
      font-weight: bold;
      color: #ffffff;
    }
    .pri-btn {
      width: 85px;
      height: 28px;
      border-radius: 80px;
      background: linear-gradient(97deg, #FFFFFF -19%, #FFFFFF 127%);
      display: flex;
      align-items: center;
      justify-content: center;
      color: #4B7BF5;
    }
  }
  .line {
    height: 1px;
    background: #F2F2F2;
    width: 100%;
    margin-top: 46rpx;
    opacity: 0.7;
  }
  .mt30px {
    margin-top: 50rpx;
  }
  .mt10px {
    margin-top: 20rpx;
  }
}
.list-title {
  color: #333333;
  font-weight: bold;
  font-size: 32rpx;
  margin-left: 32rpx;
}
.list {
  margin-top: 32rpx;
  .list-item {
    width: calc(100vw - 64rpx);
    margin-bottom: 10px;
    height: 78px;
    border-radius: 8px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    background: #ffffff;
    margin-left: 32rpx;
    padding: 24rpx;
    .list-item-left {
      display: flex;
      width: 400rpx;
      align-items: center;
      justify-content: left;
      .list-item-left-box {
        width: 100%;
        margin-left: 24rpx;
        .list-item-left-text {
          font-size: 30rpx;
          color: #333333;
          font-weight: 600;
        }
        .list-item-left-time-text {
          color: #999999;
          font-size: 24rpx;
          margin-top: 20rpx;
        }
      }
      .img {
        min-width: 64rpx;
        min-height: 64rpx;
        border-radius: 50%;
        display: flex;
        align-items: center;
        justify-content: center;
      }

    }
    .right-icon {
      margin-left: 3rpx;
    }
    .mr4px {
      margin-right: 8rpx;
    }
  }
}
</style>